package com.ruoyi.web.controller.system.api.app;

import com.ruoyi.common.config.RedisConfig;
import com.ruoyi.common.redis.RedisClientUtil;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.StInfoUpdate;
import com.ruoyi.system.api.domain.User;
import com.ruoyi.system.api.service.IUserService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ClassName UserCcController
 * @Description: TODO
 * @Author shihx
 * @Date 2020-04-07
 * @Version V1.0
 **/
@Api(tags="用户休假/用户禁止登陆")
@Controller
@RequestMapping("/api/cc/user")
public class UserCcController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private RedisConfig redisConfig;

    private static final Logger log = LoggerFactory.getLogger(UserCcController.class);


    /**
     * 用户休假接口
     * @methodName: updateWorkday
     * @description: TODO
     * @param enginnerId
     * @param workday
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-07 17:37
    **/
    @GetMapping(value = "/updateWorkday")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "用户休假接口", httpMethod = "GET", notes = "用户休假接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "休假状态（0上班、1休假）", name = "workday", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> updateWorkday(
            @RequestParam(value="enginnerId",required=true) String enginnerId,
            @RequestParam(value="workday",required=true) String workday
    ){
        try {
            if(StringUtils.isEmpty(enginnerId)){
                return new ResponseWithData().error().msg("工程师ID(enginnerId)不可以为空").code(201);
            }
            if(StringUtils.isEmpty(workday)){
                return new ResponseWithData().error().msg("休假状态(workday)不可以为空").code(202);
            }
            User user = new User();
            user.setEnginnerId(Long.parseLong(enginnerId));
            user.setWorkday(workday);
            int i = userService.updateUserByEnginnerId(user);
            return new ResponseWithData().success().msg("操作成功").code(200);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 更新用户登陆状态接口
     * @methodName: updateLoginState
     * @description: TODO
     * @param enginnerId
     * @param state
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-08 8:49
    **/
    @GetMapping(value = "/updateLoginState")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "更新用户登陆状态接口", httpMethod = "GET", notes = "更新用户登陆状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "登陆状态（0允许登陆、1禁止登陆）", name = "state", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> updateLoginState(
            @RequestParam(value="enginnerId",required=true) String enginnerId,
            @RequestParam(value="state",required=true) String state
    ){
        log.info("/updateLoginState{}{}",enginnerId,state);
        try {
            if(StringUtils.isEmpty(enginnerId)){
                return new ResponseWithData().error().msg("工程师ID(enginnerId)不可以为空").code(201);
            }
            if(StringUtils.isEmpty(state)){
                return new ResponseWithData().error().msg("登陆状态(state)不可以为空").code(202);
            }
            User user = new User();
            user.setEnginnerId(Long.parseLong(enginnerId));
            user.setState(state);
            int i = userService.updateUserByEnginnerId(user);
            // 电脑端 接口处理
            SysUser sysUser= iSysUserService.selectUserByEnginnerId(enginnerId);
            if(sysUser!=null){
                if("1".equals(state)){
                    sysUser.setStatus("1");
                   // sysUser.setDelFlag("2");
                    iSysUserService.updateUserInfo(sysUser);
                }else if("0".equals(state)){
                    sysUser.setStatus("0");
                    iSysUserService.updateUserInfo(sysUser);
                }

            }
            // 如果是禁止登陆直接从redis缓存中删除用户相关信息
            if("1".equals(state)){
                RedisClientUtil.getInstance().initialPool(redisConfig.getIp(), redisConfig.getPort(), redisConfig.getPassword(), redisConfig.getTimeout());
                Jedis redis = RedisClientUtil.getInstance().getJedis();
                /** 如果这个key在redis中存在则删除 */
                if(StringUtils.isNotEmpty(redis.get(enginnerId))){
                    String token = redis.get(enginnerId);
                    redis.del(enginnerId);
                    redis.del(token);
                }
            }
            return new ResponseWithData().success().msg("操作成功").code(200);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }




}
